package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.*;
import ModelLayer.*;
/**
 * User interface to the Rent controller              
 */

public class RentUI
{
    private RentCtr rentCtr; 
    private DVDCtr dvdCtr;
    private AddressCtr addressCtr;
    
    public RentUI()
    {
        rentCtr = new RentCtr();
        dvdCtr = new DVDCtr();
        addressCtr = new AddressCtr();
    }
    
    public void start()
    {
        rentMenu();
    }
    
    public void rentMenu()
   {
       boolean exit = false;
       while (!exit) {
           switch(writeRentMenu()) {
               case 1: getRent();
               break;
               case 2: createRent();
               break;
               case 3: deleteRent();
               break;
               case 4: updateRent();
               break;
               case 5: getPersonalRent();
               break;
               
               default: // returns to mainMenu
               exit = true;
               break;
            }
            pauseConsole();
        }
    }
    
    private int writeRentMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Rent Menu ***");
            System.out.println(" (1) Find a Rental");
            System.out.println(" (2) Create a Rental");
            System.out.println(" (3) Delete Rental");
            System.out.println(" (4) Return a Rental");
            System.out.println(" (5) Find customer rentals");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    private long inputRentId()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Rent id: ");
        long id = keyboard.nextLong();
        return id;
    }
    private long inputPersonId()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Person id: ");
        long id = keyboard.nextLong();
        return id;
    }
        
    private long inputDVDId()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD Id: " );
        long id = keyboard.nextLong();
        return id;
    }
    private int inputRentDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Date of rental: " );
        int rentDate = keyboard.nextInt();
        return rentDate;
    }
    private int inputRentDueDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Returndate: ");
        int dueDate = keyboard.nextInt();
        return dueDate;
    }
    private boolean inputRentStatus()
    {
        boolean boolStatus = true;
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Has the movie been returned: yes/no");
        String status = keyboard.nextLine();
        
        if (status.equals("yes")) {
            boolStatus = false;
        }
        return boolStatus;
    }
    
    private int inputReturnDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Rentals return date: ");
        int retDate = keyboard.nextInt();
        return retDate;
    }

  
    private void getRent()
    {     
        long id = inputRentId();
        Rent r = rentCtr.getRent(id);
       if (r != null){
       // System.out.print("Movie rented: " + rentCtr.dvdMovieTitle();
        System.out.println("Date Rented: "+ r.getRentDate());
        System.out.println("Date to be returned: "+ r.getDueDate());
        
        if (r.getStatus() != true){
        System.out.println("Movie returned:"+ r.getReturnDate());
        }
    }
    else {
        System.out.println("This identifier does not exist, please try again");
    }
        pauseConsole();
        
      
    }
    
    private void createRent()
    {
        long id = inputRentId();
        long pid = inputPersonId();
        long did = inputDVDId();
        int rentDate = inputRentDate();
        int dueDate = inputRentDueDate();
        boolean status = true;
        int returnDate = 0;
        
        rentCtr.createRent(id, pid, did, rentDate, dueDate, status, returnDate);
        
        System.out.println("This is a ticket with all the relevant information. Bum");
    }
    
    private void updateRent()
    {
        long id = inputRentId();
        boolean status = inputRentStatus();
        int returnDate = inputReturnDate();
        
        rentCtr.updateRent(id, status, returnDate);
    }
    
    private void deleteRent()
    {
        long id = inputRentId();
        
        rentCtr.deleteRent(id);
    }
    
    public void getPersonalRent()
   {
       long id = inputPersonId();
      
       ArrayList<Rent> allRents = new ArrayList<Rent>(); 
       allRents = rentCtr.getPersonalRent(id);
       
       Person p = addressCtr.getPerson(id);
       System.out.println("Customer name: " + p.getName());
       
      if (allRents != null) {
        for (Rent r : allRents){
              Copy c = r.getCopy();
              System.out.println("Rental unique id: " + r.getId());
              System.out.println("Copy Serialnumber: " + c.getSerialNo());
              System.out.println("Rented on day: " + r.getRentDate());
              System.out.println("To be returned on: " + r.getDueDate());
        }
    }else {
        System.out.println("This customer has not rented any movies");
    }
        
   }
    
    
    private void pauseConsole()
    {
        System.out.println("Press the 'Any'-key to continue");
        Scanner sc = new Scanner(System.in);
        while(!sc.nextLine().equals(""));
    }
}